package com.example.kotlin.activity

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlin.R
import com.example.kotlin.service.MyService
import kotlinx.android.synthetic.main.activity_main.bindServiceBtn
import kotlinx.android.synthetic.main.activity_main.unbindServiceBtn

class MainActivity : AppCompatActivity() {

    lateinit var downloadBinder:MyService.DownloadBinder

    private val connection=object:ServiceConnection{
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            downloadBinder=service as MyService.DownloadBinder
            downloadBinder.startDownload()
            downloadBinder.getProgess()
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            TODO("Not yet implemented")
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bindServiceBtn.setOnClickListener {
            val intent=Intent(this,MyService::class.java)
            bindService(intent,connection, Context.BIND_AUTO_CREATE)
        }
        unbindServiceBtn.setOnClickListener { unbindService(connection) }
    }
}